package com.yd.mvc.pc.permissionController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.permissionService.PermissionService;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.PermParam;
import com.yd.domain.sys.Logger;
import com.yd.domain.sys.Perm;
import com.yd.domain.sys.Role;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 11:14
 * @desc 权限controller
 **/
@RestController
@RequestMapping(value = "/api/permission")
public class PermissionController extends BaseController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc:权限的录入添加
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addPerm(@RequestBody Perm perm,HttpServletRequest request){
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            userName=user.getUserName();
        }

        String suff="code:"+perm.getCode()+";名称:"+perm.getTitle()+";描述:"+perm.getDes();
        try {
            permissionService.addPerm(perm);
            loggerService.addLogger(new Logger("新增权限记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.PERMISSION,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增权限记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.PERMISSION,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:删除权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/delete/{code}",method = RequestMethod.DELETE)
    public void deletePermByCode(@PathVariable("code") String code,HttpServletRequest request){
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            userName=user.getUserName();
        }
        String suff="code:"+code;
        try {
            permissionService.deletePermByCode(code);
            loggerService.addLogger(new Logger("删除权限记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.PERMISSION,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("删除权限记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.PERMISSION,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:修改权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public void updatePerm(@RequestBody  Perm perm,HttpServletRequest request){
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            userName=user.getUserName();
        }
        String suff="code:"+perm.getCode()+";名称:"+perm.getTitle()+";描述:"+perm.getDes();
        try {
            permissionService.updatePerm(perm);
            loggerService.addLogger(new Logger("修改权限记录",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.PERMISSION,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改权限记录",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.PERMISSION,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:根据code查询权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/find/{code}",method = RequestMethod.GET)
    public Perm findPermByCode(@PathVariable("code") String code){
        Perm perm =new Perm();
        perm= permissionService.getPermByID(code);
        return perm;
    }
    /**
     * @Author: zy
     * @Desc:分页查询数据
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getPermList(@RequestBody PermParam permParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=permissionService.getPermList(permParam,pageIndex,pageSize);
        return pm;
    }

    /**
     * 根据角色获取权限列表
     * @param roleCode
     * @return
     */
    @RequestMapping(value = "/list/roleCode/{roleCode}", method = RequestMethod.GET)
    public List<Perm> getPermList(@PathVariable("roleCode")String roleCode){
        List<Perm> pm=permissionService.getPermListByRoleCode(roleCode);
        return pm;
    }
    /**
     * 根据角色获取权限列表
     * @return
     */
    @RequestMapping(value = "/all/list", method = RequestMethod.GET)
    public List<Perm> getallPermList(){
        List<Perm> pm=permissionService.getAllPermList();
        return pm;
    }

    /**
     * 向角色添加权限
     * @return
     */
    @RequestMapping(value = "/add/permtorole/{roleCode}/{permId}", method = RequestMethod.GET)
    public void addPermToRole(@PathVariable("roleCode")String roleCode,@PathVariable("permId")String permId){
       permissionService.addPermToRole(roleCode,permId);
    }
    /**
     * 删除角色权限
     * @return
     */
    @RequestMapping(value = "/delete/permtorole/{roleCode}/{permId}", method = RequestMethod.DELETE)
    public void deletePermToRole(@PathVariable("roleCode")String roleCode,@PathVariable("permId")String permId){
        permissionService.deletePermToRole(roleCode,permId);
    }

    //获取用户角色
    @RequestMapping(value = "/getUserRoleList/{userId}", method = RequestMethod.GET)
    public List<Role> getUserRoleList(@PathVariable("userId")String userId){
        List<Role> list= permissionService.getUserRoleList(userId);
        return list;
    }
}
